//
//  YHViewController.m
//  YHNetwork
//
//  Created by 林宁宁 on 12/25/2021.
//  Copyright (c) 2021 林宁宁. All rights reserved.
//

#import "YHViewController.h"
#import "YHApp.h"
#import "YHMediatorHttp.h"
#import "YHRequest.h"
#import "YHRequest+Config.h"
#import "YHRequest+Promise.h"
#import <coobjc/coobjc.h>

@interface YHVideoInfo : NSObject
@property (copy, nonatomic) NSString * sid;
@property (copy, nonatomic) NSString * text;
@property (copy, nonatomic) NSString * video;
@property (copy, nonatomic) NSString * thumbnail;
@property (copy, nonatomic) NSString * header;
@property (copy, nonatomic) NSString * images;
@property (copy, nonatomic) NSString * name;
@end

@implementation YHVideoInfo
@end


@interface YHViewController ()

@end

@implementation YHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    [YHApp manager].mediator.http = [YHMediatorHttp new];
    
//    [self requestMethod1];
//    [self requestMethod2];
//    [self requestMethod3];
//    [self requestMethod4];
//    [self requestMethod5];
//    [self requestMethod6];
//    [self requestMethod7];
//    [self requestMethod8];
//    [self requestMethod9];
    [self requestMethod10];
}

/// 请求重试次数设置
- (void)requestMethod10{
    COPromise<YHVideoInfo *> * promise = YHRequestRegister(YHVideoInfo).GET().retryTimes(5).retryTimeInterval(3).fullurl(@"https://api.apiopen.top/getSingleJoke/error?sid=28654780").promise;
    [promise then:^id _Nullable(YHVideoInfo * _Nullable value) {
        
        return promise;
    }];
    [promise catch:^(NSError * _Nonnull error) {
        NSLog(@"重试请求都失败了");
    }];
}

/// 协程 await 链式
- (void)requestMethod9{
    co_launch(^{
        NSString * string = [YHRequest new].GET().fullurl(@"https://api.apiopen.top/recommendPoetry/err").await;
        NSLog(@"string = %@",string);
        
        YHVideoInfo * info1 = YHRequestRegister(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").await;
        NSLog(@"info1 = %@", info1);
        
        NSArray<YHVideoInfo *> * list = YHRequestRegisterList(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video").await;
        NSLog(@"list = %@", list);
        
        NSLog(@"finish");
    });
}

/// 协程 请求队列 顺序等待
- (void)requestMethod8{
    co_launch(^{
        NSString * string = await([YHRequest  new].GET().fullurl(@"https://api.apiopen.top/recommendPoetry").promise);
        NSLog(@"string = %@",string);
        
        YHVideoInfo * info1 = await(YHRequestRegister(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").promise);
        NSLog(@"info1 = %@", info1);
        
        NSArray<YHVideoInfo *> * list = await(YHRequestRegisterList(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video").promise);
        NSLog(@"list = %@", list);
        
        NSLog(@"finish");
    });
}

/// promise 设置请求先后依赖
- (void)requestMethod7{
    COPromise<YHVideoInfo *> * promise1 = YHRequestRegister(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").promise;
    COPromise<NSArray<YHVideoInfo *> *> * promise2 = YHRequestRegisterList(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video").promise;
    COPromise * promise3 = [YHRequest  new].GET().fullurl(@"https://api.apiopen.top/recommendPoetry/error").promise;
    
    [[[[promise1 then:^id _Nullable(YHVideoInfo * _Nullable value) {
        return promise2;
    }] then:^id _Nullable(id  _Nullable value) {
        return promise3;
    }] then:^id _Nullable(id  _Nullable value) {
        NSLog(@"%@",value);
        return nil;
    }] catch:^(NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

/// promise设置请求 回调信息
- (void)requestMethod6{
    COPromise<YHVideoInfo *> * promise = YHRequestRegister(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").promise;
    [promise then:^id _Nullable(YHVideoInfo * _Nullable value) {
        
        return promise;
    }];
    [promise catch:^(NSError * _Nonnull error) {
        
    }];
}

/// 泛型设置请求结果 如果为数组类型
- (void)requestMethod5{
    [YHRequestRegisterList(YHVideoInfo).fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video") executeWithSuccess:^(NSString * _Nullable message, NSArray<YHVideoInfo *> * _Nonnull result) {
        NSLog(@"%@",result);
    }];
}

/// 泛型设置请求结果需要解析的数据类型
/// 请求参数链式配置
- (void)requestMethod4{
    [YHRequestRegister(YHVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780") executeWithSuccess:^(NSString * _Nullable message, YHVideoInfo * _Nonnull result) {
        NSLog(@"%@",result);
    }];
}

/// 通过配置 设置请求 设置请求结果需要解析的数据类型
- (void)requestMethod3{
    [[YHRequest instanceConfig:^(__kindof YHRequest * _Nonnull request, YHRequestConfig * _Nonnull config) {
        config.responseClass = YHVideoInfo.class;
        config.method = YTKRequestMethodGET;
        config.baseURL = @"https://api.apiopen.top/getSingleJoke?sid=28654780";
    }] executeWithSuccess:^(NSString * _Nullable message, YHVideoInfo * _Nonnull result) {
        
    }];
}

/// 通过配置 设置请求 baseurl未域名 pathurl 路径设置请求信息
- (void)requestMethod2{
    [[YHRequest instanceConfig:^(__kindof YHRequest * _Nonnull request, YHRequestConfig * _Nonnull config) {
        config.method = YTKRequestMethodGET;
        config.baseURL = @"https://api.apiopen.top";
        config.pathURL = @"getJoke?page=1&count=2&type=video";
    }] executeWithSuccess:^(NSString * _Nullable message, id  _Nonnull result) {
        NSLog(@"%@",result);
    } failure:^(NSString * _Nullable failureMsg, NSInteger errorCode, NSError * _Nonnull error) {
        
    }];
}

/// 通过配置 设置请求 baseurl就是完整地址
- (void)requestMethod1{
    
    [[YHRequest instanceConfig:^(__kindof YHRequest * _Nonnull request, YHRequestConfig * _Nonnull config) {
        config.method = YTKRequestMethodGET;
        config.baseURL = @"https://api.apiopen.top/getJoke?page=1&count=2&type=video";
    }] executeWithSuccess:^(NSString * _Nullable message, id  _Nonnull result) {
        NSLog(@"%@",result);
    } failure:^(NSString * _Nullable failureMsg, NSInteger errorCode, NSError * _Nonnull error) {
        
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
